function fplot(sampleStru,forecast,forc,modelNames)
% =========================================================================
% =========================================================================
% fplot
%
% Creates forc plots for state variables chosen by user.
% Forecasts appended to PSC2 file with name FIGNAME
% While fig file saved as "Forecasts"+fignum
%
% fplot(sampleStru,forecast,forc,modelNames)
%
%% 1. Inputs
% modelNames                : cell of names for the models
% forc.adjustedData         :
% forc.adjustedForc         :
% forecast.forcVec          : numeric dates over which forc is done
% forecast.fig1.positions   : positions of fig1 states in cell
% forecast.scale            : 
% forecast.constants        : constants to be added back 
% 
%% Outputs
% matf                      : [nforc nser nModel] forc matrix 
% Plots found in 'Ouput' folder
% =========================================================================
cucd=pwd;

sample = sampleStru.sampleVec;
winsize = sampleStru.plotTogether;
sampfor = forecast.forcVec;
sampsubfor=sample(end-winsize+1:end);
sampsubind=(length(sample)-winsize+1):(length(sample));
forcsampcell=sample2date(sampfor);
matf = forc.adjustedForc(:,forecast.fig1.positions);
matc=forc.adjustedData(:,forecast.fig1.positions);
% matf=forc.adjustedForc(:,forecast.fig1.positions);
cnames=forecast.names(forecast.fig1.positions);
fignum='Forecasts';
consvec=forecast.constants(forecast.fig1.positions);
flag_includecons=1;
flag_plotcons=1;


nvar=size(matc,2);
nModel = size(matf,3);

plotLegend = true;
if nargin < 4
    plotLegend = false;
end


if length(cnames)~=nvar;error('SERNAMES does not match the dimension of NVAR');end;

set(0,'DefaultAxesFontSize',10);

if nvar < 4
    nc =1;
else
    nc=2 ;
end
nr  =ceil(nvar/nc);

% Plot style vectors
temp = plotAssignDefaults([]);
widthVec = temp.lineWidths;
styleVec = temp.lineStyles;
colorVec = temp.colors;

for ii=1:nvar;
    subplot(nr,nc,ii);
    % Actual Values
    
    handplot=plot(sampsubfor,matc(sampsubind,ii),'-b','LineWidth',2.5);
    
    % Loop over forecasts
    for jj = 1:nModel
        hold on;
        plot(sampfor, matf(:,ii,jj), 'LineWidth', widthVec(jj), 'Color', colorVec(jj,:),...
            'LineStyle',styleVec{jj});
        hold off;
    end
    
    % Set upper and lower limits of Y and X axis
    yoffset=0.5;
    if flag_includecons==0
        fMin = min(min(matf(:,ii,:)));
        fMax = max(max(matf(:,ii,:)));
        ymin = min([matc(sampsubind,ii);fMin]);
        ymax = max([matc(sampsubind,ii);fMax]);
        %         ymin=min(min([matc(sampsubind,ii);matf(:,ii)]));
        %         ymax=max(max([matc(sampsubind,ii);matf(:,ii)]));
        
    else
        fMin = min(min(matf(:,ii,:)));
        fMax = max(max(matf(:,ii,:)));
        ymin = min([matc(sampsubind,ii);fMin;consvec(ii)]);
        ymax = max([matc(sampsubind,ii);fMax;consvec(ii)]);
        %         ymin=min(min([matc(sampsubind,ii);matf(:,ii);consvec(ii)]));
        %         ymax=max(max([matc(sampsubind,ii);matf(:,ii);consvec(ii)]));
    end
    
    ylim([ymin-yoffset ymax+yoffset]);
    xlim([sampsubfor(1) sampfor(end)+0.05]);
    
    set(gca,'Xtick',unique(ceil( [sampsubfor;sampfor])) )    % Plot vertical line at last observable point
    hand=vline(sampfor(1));
    set(hand,'Color','r','LineWidth',2,'LineStyle','-');
    
    % Plot horizontal line at SS values
    if flag_plotcons~=0
        hand=hline(consvec(ii));
        set(hand,'Color',[0.55 0.55 0.55],'LineWidth',1,'LineStyle','--');
    end
    title(cnames{ii},'FontSize',12,'FontName','Helvetica');
end

% ---------------------------------------
% Title above all graphs
% ---------------------------------------
set(gcf,'PaperPosition',[0.75 0.5 7.5 10]);
hand=suptitle(['Figure 1: Forecasts starting ',forcsampcell{2}]);
set(hand,'FontSize',16,'FontName','Helvetica');
%hand=subtitulo('Vertical red line corresponds to the date of last data point');
%set(hand,'FontSize',8,'FontName','Helvetica');

if plotLegend
    h=legend(['Data',modelNames],'Orientation','horizontal');
    set(h, 'Position', [0.42, 0.025, 0.19, 0.038]);
end

print(gcf,'-dpdf',fullfile(sampleStru.savePath,'figure1'),'-append');
%set(hand,'FontSize',8,'FontName','Helvetica');
% saveas(gcf,'fig1','fig');

